package com.aiyige.page.follow;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.api.ApiManager;
import com.aiyige.model.Page;
import com.aiyige.model.sns.Follow;
import com.aiyige.page.follow.adapter.FollowAdapter;
import com.aiyige.utils.widget.CommonDataView;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;

@Route(path = ARouterConfig.FollowPage)
public class FollowPage extends AppCompatActivity implements CommonDataView.RetrofitStub {

    @BindView(R.id.titleBackBtn)
    ImageView titleBackBtn;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.cdv)
    CommonDataView cdv;
    FollowAdapter followAdapter;
    @Autowired
    String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_fans);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        titleTv.setText(R.string.follow);
        followAdapter = new FollowAdapter();
        cdv.config(followAdapter, this);
        cdv.doRefreshRequest();
    }

    @OnClick({R.id.titleBackBtn})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
        }
    }

    @Override
    public Call<ResponseBody> call(long currentPage) {
        return ApiManager.getService().getFollowList(userId, 15, currentPage);
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            Page page = JSON.parseObject(respJson, Page.class);
            List<Follow> followList = new LinkedList<>();
            if (!TextUtils.isEmpty(page.getContent())) {
                List<Follow> tempFollowList = JSON.parseArray(page.getContent(), Follow.class);
                for (Follow follow : tempFollowList) {
                    followList.add(new Follow(follow));
                }
            }
            resultList.addAll(followList);
            return new CommonDataView.HandleResult(page.isLast());
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }
    }
}
